package mamikeji.liaowa.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import mamikeji.liaowa.R;
import mamikeji.liaowa.ui.adapter.WelcomePageAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;

/**
 * Created by 廉冲-android开发 on 2017/5/5.
 */
public class WelcomeActivity extends BaseActivity {
    private static final String TAG = "WelcomeActivity";
    private ViewPager vp;
    private WelcomePageAdapter vpAdapter;

    //引导图片资源
    private static final int[] pics = {
            R.drawable.image_welcome_1,
            R.drawable.image_welcome_2,
            R.drawable.image_welcome_3,
            R.drawable.image_welcome_4};

    //记录当前选中位置
    private int currentIndex;

    private LinearLayout linearLayout_welcome_login;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        getFirst();

        init();
    }

    private void init() {
        vp = (ViewPager) findViewById(R.id.main_picturecarousel_vp);
        linearLayout_welcome_login = (LinearLayout) findViewById(R.id.linearLayout_welcome_login);

        ArrayList<ImageView> views = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(this);
        for (int i = 0; i < pics.length; i++) {
            ImageView imgv = new ImageView(this);
            imgv.setImageResource(pics[i]);
            imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            views.add(imgv);
        }
        vp.setAdapter(new WelcomePageAdapter(this, views));
        vp.setCurrentItem(0);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                linearLayout_welcome_login.setVisibility( position%1000%4 == 3 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        linearLayout_welcome_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(WelcomeActivity.this,FistActivity.class));
                finish();
            }
        });
    }


    public void getFirst() {
        ForPreference.putBoolean(ForContents.GET_FIRST, true);
        ForPreference.putBoolean(ForContents.GET_NEW, true);//是否开启消息通知
        full(true);
    }

    /**
     * false 显示
     * true 隐藏
     */
    private void full(boolean enable) {
        WindowManager.LayoutParams p = this.getWindow().getAttributes();
        if (enable) {
            p.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            p.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        getWindow().setAttributes(p);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

}